package com.hxkj.controller;

import com.hxkj.domain.po.AllocationFunction;
import com.hxkj.domain.po.AuthRole;
import com.hxkj.service.AuthRoleService;
import com.platform.common.dispatcher.util.ResultStructUtils;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * 角色控制器
 * <p>
 * Created by chenfangzhou on 2018/5/15.
 */
@RestController
@RequestMapping("/authrole")
public class AuthRoleController {

    @Autowired
    private AuthRoleService authRoleService;


    /**
     * 获取角色列表
     *
     * @param parameterDataStruct 角色列表
     * @return 翻页数据
     * @throws Exception
     */
    @RequestMapping(value = "/getlist", method = RequestMethod.POST)
    public Callable<ResultPageStruct<AuthRole>> getRoleList(@RequestBody ParameterDataStruct<AuthRole> parameterDataStruct) throws Exception {
        return () -> ResultStructUtils.buildResultPageStruct(authRoleService.getRoleList
                (parameterDataStruct.getData() == null ? new AuthRole() : parameterDataStruct.getData(), parameterDataStruct.getPageTo()), "param=value");
    }

    /**
     * 获取角色列表
     *
     * @param parameterDataStruct 从从库获取角色列表
     * @return 翻页数据
     * @throws Exception
     */
    @RequestMapping(value = "/getlist/slave", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultPageStruct<AuthRole>> getUserBySlave(@RequestBody ParameterDataStruct<AuthRole> parameterDataStruct) throws Exception {
        return () -> ResultStructUtils.buildResultPageStruct(authRoleService.getAuthRoleBySlave
                (parameterDataStruct.getData() == null ? new AuthRole() : parameterDataStruct.getData(), parameterDataStruct.getPageTo()), "param=value");
    }

    /**
     * 获取单个角色
     *
     * @param authRole
     * @return authRole
     * @throws Exception
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<AuthRole>> getAuthRole(@RequestBody AuthRole authRole) throws Exception {
        return () -> authRoleService.getAuthRole(authRole);
    }

    /**
     * 新增角色
     *
     * @param authRole
     * @throws Exception
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public ResultMessageStruct saveAuthRole(@RequestBody AuthRole authRole) throws Exception {
        return authRoleService.saveAuthRole(authRole);
    }

    /**
     * 删除单个角色
     *
     * @param authRole
     * @throws Exception
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public ResultMessageStruct deleteAuthRole(@RequestBody AuthRole authRole) throws Exception {
        return authRoleService.deleteAuthRole(authRole);
    }

    /**
     * 更新角色信息
     *
     * @param authRole
     * @throws Exception
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public ResultMessageStruct updateAuthRole(@RequestBody AuthRole authRole) throws Exception {
        return authRoleService.updateAuthRole(authRole);
    }

    /**
     * 获取该角色已分配的功能/全部功能
     *
     * @param authRole 角色
     * @return 已分配的功能/全部功能
     * @throws Exception
     */
    @RequestMapping(value = "/getfunction", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<AllocationFunction>> getFunction(@RequestBody AuthRole authRole) throws Exception {
        return () -> authRoleService.getFunction(authRole);
    }

    /**
     * 获取该角色已分配的功能/全部功能(树形结构)
     *
     * @param authRole 角色
     * @return 已分配的功能/全部功能(树形结构)
     * @throws Exception
     */
    @RequestMapping(value = "/getfunctions", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<AllocationFunction>> getFunctions(@RequestBody AuthRole authRole) throws Exception {
        return () -> authRoleService.getFunctions(authRole);
    }

    /**
     * 为该角色分配功能
     *
     * @param authRole 角色
     * @return 已分配的功能/全部功能
     * @throws Exception
     */
    @RequestMapping(value = "/setfunction", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8")
    public ResultMessageStruct allocationFunction(@RequestBody AuthRole authRole) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            authRoleService.saveAllocationFunction(authRole);
            resultMessageStruct.setCode(200);
            resultMessageStruct.setMessage("成功");
        } catch (Exception e) {
            resultMessageStruct.setCode(400);
            resultMessageStruct.setMessage(e.getMessage());
        }
        return resultMessageStruct;
    }
}
